package severlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.ibatis.session.SqlSession;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import dao.PassageMapper;
import model.Passage;
import util.SqlSessionUtil;

/**
 * Servlet implementation class SreachServlet
 */
@WebServlet("/UpdateServlet")
/**
* @author 听风寂  ，林舒馨
* @date 2020/1/8
* @ClassName UpdateServlet
*/
public class UpdateServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public UpdateServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		//解决后端向前端传数据中文乱码问题
		response.setCharacterEncoding("UTF-8");
		String idStr = request.getSession().getAttribute("reading").toString(); 
		//通过id获取文章
		int id=Integer.parseInt(idStr);
		SqlSession s = SqlSessionUtil.getSqlSession();
		PassageMapper up = s.getMapper(PassageMapper.class);
		Passage passage = up.selectOne(id);
		JSONObject object=new JSONObject();
		PrintWriter out = response.getWriter();  
		object.put("passage",passage);
		String jsonStr = JSON.toJSONString(object);
		out.print(jsonStr);
		out.close();
		String title = request.getParameter("title");
		System.out.println(title);
		String body = request.getParameter("body");
		System.out.println(body);
		Passage newPassage = new Passage(title, body);
		int flag = up.upPassage(newPassage,id);
		System.out.println(flag);
		s.commit();
		
		
	}
	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
